package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.woniu_bx.mapper.ChatMapper;
import com.woniu.woniu_bx.pojo.Chat;
import com.woniu.woniu_bx.pojo.Order;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @ClassName ChatServiceImpl
 * @Description 聊天记录 Service接口实现类
 * @Author 魏才林
 * @Date 2022/8/27 16:32
 * @Version 1.0
 **/

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Override
    public void add(Chat chat) {
        //（1）聊天时间暂时精确到：年月日、时分秒
        SimpleDateFormat dateTimeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        chat.setTime(dateTimeformat.format(System.currentTimeMillis()));

        chatMapper.insert(chat);
    }

    @Override
    public PageBean<Chat> getChatRecord(Integer myId, Integer otherId, Integer pageSize, Integer pageNum) {

        PageBean<Chat> pageBean = new PageBean<>();
        Page p = PageHelper.startPage(pageNum, pageSize);

        List<Chat> chatList = chatMapper.findChatList(myId, otherId);

        pageBean.setData(chatList);
        pageBean.setCurrPage(pageNum);
        pageBean.setCurrPageSize(chatList.size());
        pageBean.setPageSize(pageSize);
        pageBean.setTotalNums((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
        return pageBean;
    }

    @Override
    public List<Chat> findChatList(Integer myId, Integer otherId) {
        return chatMapper.findChatList(myId, otherId);
    }

    /**
     * 獲取列表
     * @param id
     * @return
     */
    @Override
    public List<Integer> getChatList(int id) {
        return chatMapper.getChatList(id);
    }
    /**
     * 根据发送人和收信人的id查询未读的条数
     * @param sendId
     * @param acceptId
     * @return
     */
    @Override
    public Integer getSumUnReadChatNum(Integer sendId, Integer acceptId) {
        return chatMapper.sumUnReadChat(sendId,acceptId);
    }
    /**
     * 根据发送人和收信人的id查询未读的消息
     * @param myId
     * @param otherId
     * @return
     */
    @Override
    public List<Chat> findChatListUnRead(Integer myId, Integer otherId) {
        return chatMapper.findChatListUnRead(myId,otherId);
    }
}
